package common.utils

import android.content.Context

/**
 * Author : zofnk.
 * Email : zofnk@vip.qq.com.
 * Creat Time :  2019-11-11. 11:21
 */
class DpUtil {

    companion object {
        /**
         * dp转换px
         */
        @JvmStatic
        fun dp2px(context: Context, dip: Float): Int {
            val scale = context.resources.displayMetrics.density
            return (dip * scale + 0.5f).toInt()
        }

        /**
         * px转换dp
         */
        @JvmStatic
        fun px2dp(context: Context, px: Int): Int {
            val scale = context.resources.displayMetrics.density
            return (px / scale + 0.5f).toInt()
        }

        /**
         * 将px值转换为sp值，保证文字大小不变
         */
        @JvmStatic
        fun px2sp(context: Context, pxValue: Float): Int {
            val fontScale = context.resources.displayMetrics
                .scaledDensity
            return (pxValue / fontScale + 0.5f).toInt()
        }

        /**
         * 将sp值转换为px值，保证文字大小不变
         */
        @JvmStatic
        fun sp2px(context: Context, spValue: Float): Int {
            val fontScale = context.resources.displayMetrics
                .scaledDensity
            return (spValue * fontScale + 0.5f).toInt()
        }
    }
}